package logic

import (
	"B/shenzhou_srv/internal/svc"
	"B/shenzhou_srv/model/mysql"
	"B/shenzhou_srv/shenzhou"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginAndRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginAndRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginAndRegisterLogic {
	return &UserLoginAndRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginAndRegisterLogic) UserLoginAndRegister(in *shenzhou.UserLoginAndRegisterRequest) (*shenzhou.UserLoginAndRegisterResponse, error) {
	// todo: add your logic here and delete this line
	var u mysql.User
	u.Find(in.Username)
	if u.ID == 0 {
		u = mysql.User{
			Username: in.Username,
			Password: in.Password,
			Status:   in.Status,
		}
		u.Create()
	}
	if u.Password != in.Password {
		logx.Error("密码错误")
		return nil, fmt.Errorf("密码错误")
	}

	return &shenzhou.UserLoginAndRegisterResponse{UserId: int64(u.ID)}, nil
}
